import {reactive, watch} from "vue";
import {useRoute} from "vue-router";

export default function useGoodsFilterOptions() {
    const filterOption = reactive({
        time: useRoute().query.time ? useRoute().query.time : 'month',
        year: useRoute().query.year ? useRoute().query.year : '2021',
        stats: useRoute().query.stats ? useRoute().query.stats : 'cum',
        type: useRoute().query.type ? useRoute().query.type : 'total'
    })

    // 定义年份筛选项
    const yearOptions = reactive([
        {value: '2021', label: '2021',},
        {value: '2020', label: '2020',},
        {value: '2019', label: '2019',}
    ])

    // 定义时间维度筛选项
    const timeDimensionOptions = reactive([
        {value: 'year', label: '年',},
        {value: 'quarter', label: '季',},
        {value: 'month', label: '月',}
    ])

    // 定义统计维度筛选项
    const statsOptions = reactive([
        {value: 'cum', label: '累计',},
        {value: 'month', label: '本月',},
    ])

    // 定义对比类型筛选项
    const typeOptions = reactive([
        {value: 'total', label: '合计'},
        {value: 'fre', label: '外贸'},
        {value: 'out', label: '出口'},
        {value: 'in', label: '进口'},
        {value: 'freOut', label: '外贸出口'},
        {value: 'freIn', label: '外贸进口'}
    ])


    watch(filterOption, ()=> {
        // 筛选规则，如果时间维度的选项为年，那么统计维度选项只能为累计
        if (filterOption.time === 'year') {
            filterOption.stats = 'cum'
        }
        // 文字优化，根据时间维度的选择，statsOption的label转变成本年、本季、本月
        statsOptions[1].label = '本' + timeDimensionOptions.filter(item => item.value === filterOption.time)[0].label
    })

    return {
        filterOption,
        yearOptions,
        timeDimensionOptions,
        statsOptions,
        typeOptions,
    }
}